from flask import Flask, Response
import time

app = Flask(__name__)


# 允许跨域（仅测试用）
@app.after_request
def add_cors_headers(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response


@app.route('/chunked-image')
def chunked_image():
    # 图片路径（替换为你的图片）
    image_path = 'E:\\下载\\1.png'

    # 分块传输生成器
    def generate():
        chunk_size = 1024 * 1  # 每块16KB（可调整）
        with open(image_path, 'rb') as f:
            while True:
                chunk = f.read(chunk_size)
                if not chunk:
                    break
                # 模拟慢速传输（每块延迟0.1秒）
                time.sleep(0.5)
                yield chunk

    # 返回分块响应
    return Response(
        generate(),
        mimetype='image/jpeg',
        headers={'Transfer-Encoding': 'chunked'}
    )


if __name__ == '__main__':
    app.run(port=4000)